package com.h3c.nem.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.h3c.nem.utils.AppBusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@SuppressWarnings("NonAsciiCharacters")
@Getter
@AllArgsConstructor
public enum OfferWorkerStatusEnum {
    offer审批中(1, "offer审批中"),
    待发放offer(2, "待发放offer"),
    待填写入职资料(3, "待填写入职资料"),
    待补充资料(4, "待补充资料"),
    待审核入职资料(5, "待审核入职资料"),
    待签合同(6, "待签合同"),
    合同签署中(7, "合同签署中"),
    已签署合同(8, "电子合同审核中"),
    签署完成(9, "合同已签署"),
    域账号生成失败(10, "域账号生成失败(工号已生成)"),
    已入职(11, "已入职"),
    拒绝签署合同(12, "拒绝签署合同"),
    取消入职(13, "取消入职"),
    放弃入职(14, "放弃入职"),
    签署异常(55, "签署异常"),
    ;

    @EnumValue
    @JsonValue
    private final Integer code;
    private final String name;

    /**
     * code 作为 key，封装为 Map
     */
    private static final Map<Integer, OfferWorkerStatusEnum> CODE_MAP = Stream
            .of(OfferWorkerStatusEnum.values())
            .collect(Collectors.toMap(OfferWorkerStatusEnum::getCode, Function.identity()));


    public static String getNameByCode(OfferWorkerStatusEnum code) {
        for (OfferWorkerStatusEnum workerStatusEnum : OfferWorkerStatusEnum.values()) {
            if (workerStatusEnum.equals(code)) {
                return workerStatusEnum.getName();
            }
        }
        return "";
    }

    public static void lessCode(OfferWorkerStatusEnum code) {
        if (code.getCode() > OfferWorkerStatusEnum.待签合同.getCode()) {
            if (!code.equals(OfferWorkerStatusEnum.取消入职)
                    && !code.equals(OfferWorkerStatusEnum.放弃入职)
                    && !code.equals(OfferWorkerStatusEnum.签署异常)) {
                throw new AppBusinessException("预备员工当前状态不支持取消入职");
            }
        }
    }

    public static void lessCodeEditInformation(OfferWorkerStatusEnum code) {
        if (code.getCode() > OfferWorkerStatusEnum.合同签署中.getCode()) {
            throw new AppBusinessException("预备员工当前状态不支持编辑资料");
        }
    }

    public static boolean lessCodeEditInformationNoException(OfferWorkerStatusEnum code) {
        return code.getCode() <= OfferWorkerStatusEnum.签署完成.getCode();
    }


    /**
     * 静态工厂反序列化
     *
     * @param key code
     * @return 评论的类型枚举
     */
    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static OfferWorkerStatusEnum valueOfKey(Integer key) {
        return Optional.ofNullable(CODE_MAP.get(key))
                .orElseThrow(() -> new IllegalArgumentException(String.valueOf(key)));
    }

    public static List<OfferWorkerStatusEnum> valueOfKey(List<Integer> keys) {
        List<OfferWorkerStatusEnum> list = new ArrayList<>();
        for (Integer key : keys) {
            OfferWorkerStatusEnum offerWorkerStatusEnum = Optional.ofNullable(CODE_MAP.get(key))
                    .orElseThrow(() -> new IllegalArgumentException(String.valueOf(key)));
            list.add(offerWorkerStatusEnum);
        }
        return list;
    }

}
